package com.autumn.demo;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;

import com.autumn.demo.cfg.UserTestSessionImpl;
import com.autumn.mybatis.annotation.EnableAutumnMybatisDynamicDataSource;
import com.autumn.mybatis.factory.DefaultDynamicPropertiesDataSourceFactory;
import com.autumn.mybatis.factory.DynamicRequestServiceContextRoutingDataSource;
import com.autumn.runtime.session.IAutumnSession;
import com.autumn.swagger.AutumnSwaggerHeaderParameter;
import com.autumn.swagger.EnableAutumnSwagger;
import com.autumn.web.ContextServiceRequestHeader;

import io.swagger.annotations.Api;

/**
 * 
 * @author 老码农
 *
 *         2018-01-08 12:28:58
 */
@ComponentScan({ "com.autumn.demo" }) // bean组件扫描范围
@Api(tags = "动态数据源")
@EnableAutumnSwagger(value = { "com.autumn.demo.controllers" }, headerParameters = {
		@AutumnSwaggerHeaderParameter(name = ContextServiceRequestHeader.HEADER_DEFAULT_PARAMETER_NAME, description = "上下文服务") })
//@EnableAutumnConfigClient
// @EnableAutumnRedisSession
// @EnableEurekaClient
// @EnableCircuitBreaker
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })
@EnableAutumnMybatisDynamicDataSource(value = { "com.autumn.demo.repositories" }, typeAliasesPackages = {
		"com.autumn.demo.entities" }, dataSourceFactoryType = DefaultDynamicPropertiesDataSourceFactory.class, dataSourceRoutingType = DynamicRequestServiceContextRoutingDataSource.class)
// @EnableServiceRequestContext
public class DemoDynamicDataSourceAppliation extends AbstractAutumnDemoBootApplication implements HandlerInterceptor {
	public static void main(String[] args) {
		run(DemoDynamicDataSourceAppliation.class, args);
	}

	@Bean
	@Override
	public UserTestSessionImpl autumnSession() {
		return new UserTestSessionImpl();
	}

	@Autowired
	private IAutumnSession session;

	// 以下仅作为测试自动会话

	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		super.addInterceptors(registry);
		registry.addInterceptor(this);
	}

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		session.getIdentityClaims();
		return true;
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {

	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {

	}

}
